using System;
using Slaks.DocumentSerialization.Dictionary;
using Slaks.DocumentSerialization.Document;

namespace Slaks.Graphs.GraphBuilder.SchenkerDictionary
{
	/// <summary>
	/// Summary description for NormalizedFrequencyRepresentationSlaks.
	/// </summary>
	public class NormalizedFrequencyDictionaryRepresentationSchenker : SchenkerDictionaryBuilder, IDictionaryRepresentation
	{
		#region IDictionaryRepresentation Members

		public Graph Build(Document document, DictionaryMap dictionaryMap)
		{
			string documentName = document.FileName;
			int index = documentName.LastIndexOfAny(new char[] {'\\', '/'});
			if (index >= 0 && (index + 1 < documentName.Length)) documentName = documentName.Substring(index + 1);

			Graph graph = new Graph(documentName);

			Vertex vertex;
			string sectionType;

			long frequentNode = 0;

			#region find frequent node

			long mostFrequentId = document.MostFrequencyInfo.TotalMostFrequentId;
			frequentNode = document.GetNodeById(mostFrequentId).TotalTermFrequency;

			#endregion

			//iterate through nodes
			foreach (Node node in document.Nodes)
			{
				//check if the vertex is in the list of frequent nodes from dictionary
				if (dictionaryMap.Contains(node.Id) == false) continue;
				vertex = graph.CreateAddVertex(node.Id);
				vertex.Frequency = (double) node.TotalTermFrequency/frequentNode;

				//iterate through sections
				foreach (Section section in node.Sections)
				{
					sectionType = section.GetSectionTypeString();

					Section.SectionType sectionTp = Section.GetSectionTypeByString(sectionType);

					//iterate through sentences
					foreach (Sentence sentence in section.Sentences)
					{
						AddRelations(document, graph, dictionaryMap, vertex, 1, 1, sentence, sectionTp, BuilderType.NormalizedFrequencyDictionaryRepresentationSchenker);
					}
				}
			}

			#region set normalized frequency to the edge

			VertexList vertexList = graph.GetVertexList();
			double mostFrequentEdge = long.MinValue;
			double tempEdgeFrequency = 0;
			foreach (Vertex ver in vertexList)
			{
				tempEdgeFrequency = 0;
				foreach (EdgeList edgeLst in ver.GetSection2EdgeMap().Values)
				{
					foreach (Edge edge in edgeLst)
					{
						tempEdgeFrequency += edge.GetFrequency();
					}
				}

				if (mostFrequentEdge < tempEdgeFrequency) mostFrequentEdge = tempEdgeFrequency;
			}

			//set the normalized frequency
			foreach (Vertex ver in vertexList)
			{
				foreach (EdgeList edgeLst in ver.GetSection2EdgeMap().Values)
				{
					foreach (Edge edge in edgeLst)
					{
						edge.SetFrequency(edge.GetFrequency()/mostFrequentEdge);
					}
				}
			}

			#endregion

			return graph;
		}

		public Graph Build(Document document, int nParameter, DictionaryMap dictionaryMap)
		{
			throw new NotImplementedException("NormalizedFrequencyDictionaryRepresentationSlaks does not implement this method");
		}

		#endregion
        protected override string GetBuilderName()
        {
            return "Schenker's Normalized Frequency By Dictionary Representation";
        }
        protected override string GetBuilderDescription()
        {
            return "The graph nodes are labeled with the normalized frequency of the term (instead of the actual count). Only terms that are most frequent throughout the collection are used.";
        }
	}
}